package com.whz.parameterized;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

import java.lang.reflect.Type;
import java.util.*;

/**
 * @Author: sinosoft
 * @Description: List 与 List<Object> 的区别
 * @Date: Create in 14:16 2021/5/11
 * @Modified By:
 */
public class MainTest<T> {
    private int size;
    private Object[] elementData;
    public static void main(String[] args) {

        Stack1<Number> numStack = new Stack1<>();
        Stack1<Integer> intStack = new Stack1<>();
        intStack.push(1);
        intStack.push(2);
        intStack.push(3);
        intStack.push(4);
        numStack.pushAll(intStack);
//        String[] strings = {"jute", "hemp", "nylon"};

//        Comparator<Object> objectComparator = Collections.reverseOrder();
//        List<String> nameList = List.of("whz", "sxm", "jkb");
//        List<String> stringList = Lists.newArrayList("whz", "sxm", "jkb");
//        Collections.sort(stringList, objectComparator);
//        Collections.sort(stringList);
//        System.out.println(stringList);

//        Set<String> setSet = Collections.emptySet();
//        setSet.add("whz");

//        Class<? extends Set> clazz = nameSet1.getClass();
//        Type type = clazz.getGenericSuperclass();
//
//        System.out.println(type);
//        System.out.println(nameSet1.getClass().getClasses().toString());
//        Set<String> nameSet2 = Set.of("zzy");
//        Set result = unionSafe(nameSet1, nameSet2);
//        System.out.println(result);
//        List<String> strList = Lists.newArrayList();
//        strList.add("zzy");
//        strList.add("jkb");
//        strList.toArray(new String[]{"whz", "sxm", "syy"});
//        System.out.println(strList);
//        List<Object> objList = Lists.newArrayList();
        /*
        * List<String>是原生态类型List的一个子类型 而不是参数化类型List<Object>的子类型
        * */
        /*List list = strList;
        List<Object> objList = strList;*/
//        unsafeAdd(objList, Integer.valueOf(4));
//        Object o = objList.get(0);
//        System.out.println(o);

//        Object[] objArr = new Long[1];
//        objArr[0] = "I don`t fit in";
//
//        List<Long> objList = Lists.newArrayList();
//        objList.add("");
        /*创建泛型数组 实际上是不合法的*/
//        List<String>[] stringLists =  new ArrayList<String>[1];
//        List<Integer> intList = List.of(42);
//        Object[] objects = stringLists;
//        objects[0] = intList;
//        String s = stringLists[0].get(0);
        /*创建无限制通配类型的数组是合法的*/
//        List<?>[] listArray = new ArrayList<?>[10];
//        listArray[0] = Lists.newArrayList("whz", "jkb", "zzy");
//        listArray[1] = Lists.newArrayList(100d, 200d, 300d);
//        listArray[2] = Lists.newArrayList(100, 200, 300);
//        System.out.println(listArray[0].get(2) instanceof Integer);

//        List<String> strList = Lists.newArrayList("whz");
//        String s = strList.get(0);
//        System.out.println();



    }

    private static void unsafeAdd(List list, Object o) {
        list.add(o);
    }
    public <T> T[] toArray(T[] a) {
        if (a.length < size) {
            // Make a new array of a's runtime type, but my contents:
            @SuppressWarnings("unchecked")
            T[] result = (T[]) Arrays.copyOf(elementData, size, a.getClass());
            return result;
        }
        System.arraycopy(elementData, 0, a, 0, size);
        if (a.length > size)
            a[size] = null;
        return a;
    }

    public static Set union(Set s1, Set s2) {
//        s1.addAll(s2);
//        Objects.requireNonNull(s1)
//        @SuppressWarnings("unchecked")
        Set result = Sets.newHashSet(s1);
        result.addAll(s2);
        return result;
    }

    public static <E> Set unionSafe(Set<E> s1, Set<E> s2) {
//        s1.addAll(s2);
//        Objects.requireNonNull(s1)
//        @SuppressWarnings("unchecked")
//        Set<E> result = Sets.newHashSet(s1);
//        result.addAll(s2);
//        return result;
        s1.addAll(s2);
        return s1;
    }
}
